package com.hue.springbootneo4j.utils;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * 将map集合转换为指定的类
 */
public class TypeConverter {
    public static <T> T parseObject(Map<String, Object> map, Class<? extends T> aClass) {
        try {
            T bean = aClass.newInstance();
            Field[] fields = aClass.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                if (map.containsKey(name)) {
                    field.setAccessible(true);
                    field.set(bean, map.get(name));
                }
            }
            return bean;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
